package tree;

import java.util.LinkedList;
import java.util.Queue;

/**
 * Created with IntelliJ IDEA.
 * Description: 二叉树
 * User: 王圆豪
 * Date: 2024-09-07
 * Time: 23:16
 */
class Tree{
    char val;
    Tree left;
    Tree right;

    public Tree(char val) {
        this.val = val;
    }
}

public class binaryTree {
    public static void main(String[] args) {
        Tree root = createTree();
        System.out.print("前序遍历：");
        preTraversal(root);
        System.out.println();
        System.out.print("中序遍历：");
        inorderTraversal(root);
        System.out.println();
        System.out.print("后序遍历：");
        posTraversal(root);
        System.out.println();
        System.out.print("二叉树的节点个数：");
        System.out.println(size(root));
        System.out.print("二叉树中叶子节点的个数：");
        System.out.println(getLeafNodeCount(root));
        System.out.print("二叉树第k层的节点个数：");
        System.out.println(getKLevelNodeCount(root, 2));
        System.out.print("二叉树的高度：");
        System.out.println(getHeight(root));
        System.out.print("二叉树中是否有元素val：");
        System.out.println(find(root, 'f'));
        System.out.print("二叉树的层序遍历：");
        levelOrder(root);
        System.out.println();
        System.out.print("该二叉树是不是完全二叉树：");
        System.out.println(isCompleteTree(root));
    }

    public static Tree createTree(){
        Tree A = new Tree('A');
        Tree B = new Tree('B');
        Tree C = new Tree('C');
        Tree D = new Tree('D');
        Tree E = new Tree('E');
        Tree F = new Tree('F');
        Tree G = new Tree('G');
        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        return A;
    }
    //前序遍历：
    public static void preTraversal(Tree tree){
        if (tree == null) return;

        System.out.print(tree.val + " ");
        preTraversal(tree.left);
        preTraversal(tree.right);
    }

    //中序遍历：
    public static void inorderTraversal(Tree tree){
        if (tree == null) return;
        inorderTraversal(tree.left);
        System.out.print(tree.val + " ");
        inorderTraversal(tree.right);
    }

    //后序遍历：
    public static void posTraversal(Tree tree){
        if (tree == null) return;
        posTraversal(tree.left);
        posTraversal(tree.right);
        System.out.print(tree.val + " ");
    }

    //获取树中节点的个数
    public static int size(Tree root){
        if (root == null) return 0;
        return size(root.left) + size(root.right) + 1;
    }

    //获取叶子节点的个数
    public static int getLeafNodeCount(Tree root){
        if (root == null) return 0;
        if (root.left == null && root.right == null){
            return 1;
        }
        return getLeafNodeCount(root.left) + getLeafNodeCount(root.right);
    }

    //获取第K层节点的个数
    public static int getKLevelNodeCount(Tree root,int k){
        if (root == null) return 0;
        if (k == 1) return 1;
        return getKLevelNodeCount(root.left, k - 1)
                + getKLevelNodeCount(root.right, k - 1);
    }
    //获取二叉树的高度
    public static int getHeight(Tree root){
        if (root == null) return 0;
        int highLeft = getHeight(root.left);
        int highRight = getHeight(root.right);
        return Math.max(highLeft, highRight) + 1;
    }

    //检测值为value的元素是否存在
    public static boolean find(Tree root, char val){
        if (root == null) return false;

        if (root.val == val) return true;
        return find(root.left, val) || find(root.right, val);
    }

    //层序遍历
    public static void levelOrder(Tree root){
        Queue<Tree> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()){
            Tree cur = queue.poll();
            System.out.print(cur.val+ " ");
            if (cur.left != null) queue.offer(cur.left);
            if (cur.right != null) queue.offer(cur.right);
        }
    }

    //判断一棵树是不是完全二叉树
    public static boolean isCompleteTree(Tree root){
        Queue<Tree> queue = new LinkedList<>();
        queue.offer(root);
        Tree cur = root;
        while (cur != null){
            queue.offer(cur.left);
            queue.offer(cur.right);
            cur = queue.poll();
        }
        return isAllNull(queue);
    }

    public static boolean isAllNull(Queue<Tree> queue){
        while (!queue.isEmpty()){
            Tree cur = queue.poll();
            if (cur != null) return false;
        }
        return true;
    }
}
